package com.example.demo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RestController
@RequestMapping("/user")
public class LoginController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        // 1. 参数校验
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        // 2. 检验用户名和密码
        if(("admin".equals(userName) || "zhangsan".equals(userName)) && "admin".equals(password)){
            session.setAttribute("userName", userName);
            return true;
        }

        return false;
    }

    @RequestMapping("/getLoginUser")
    public String getLoginUser(@SessionAttribute("userName") String userName){
        return userName;
    }
}
